#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include<pthread.h>
#include "bluetooth.h"
#include "public.h"

/**
 * __do_abnormal - exception handling.
 *	@str : function name
 */
void __do_abnormal(const char * str)
{
    printf("%s abnormal, program terminated...\n", str);
    exit(0);
}


/*
 * abstruct : main program.
 */

int main(int argc, char ** argv)
{
	int com;
	unsigned char buffer[] = "123";
	pthread_t com_read_id;

	void * ptr;
	pthread_attr_t attr;

	if (argc < 2) {
		printf("usage: %s 0|1|2|3.\n", argv[0]);
		exit(0);
	}

	printf("argc: %d\n", argc);

	/* system("insmod pwm_led.ko"); */
	
	com = atoi(argv[1]);
	uart_init(com, 115200, 8, 1, 'n');	

	
	bluetooth_at_mode();
	msleep(1000);
	bluetooth_set_uart();
	msleep(1000);

    #if 0
	if (argc >= 3) {
		if (strncmp(argv[2], "setmac", 6) == 0) {	
			bluetooth_get_mac();
			msleep(1000);
			bluetooth_set_mac(atoi(argv[3]) & 0xff, atoi(argv[4]) & 0xff);
			msleep(1000);
			bluetooth_get_mac();
			msleep(1000);
			BluetoothChipReset();
			msleep(2000);
		} 
	
		if (strncmp(argv[2], "getmac", 6) == 0) {	
			bluetooth_get_mac();
			msleep(1000);		
		}

		if (strncmp(argv[2], "setmode", 7) == 0) {	
			BluetoothBroadcastMode(atoi(argv[3]));
			msleep(1000);		
		}

		if (strncmp(argv[2], "getmode", 7) == 0) {	
			BluetoothGetWorkMode();
			msleep(1000);		
			BluetoothChipReset();
			msleep(1000);
		}
	}
	#endif

	#if 0
	pthread_attr_init(&attr);

	com_read_id = 0;
	pthread_create(&com_read_id, &attr, uart_receive_thread_none, NULL);
	msleep(1000);
	#endif	

	/* for send test */
	while (1) {
		//uart_send(buffer, sizeof(buffer));
        bluetooth_at_mode();
        msleep(1000);
        #if 0
		bluetooth_chip_version();
		msleep(1000);
		bluetooth_get_serinum();
		msleep(1000);
		bluetooth_get_sysid();
		msleep(1000);
        #endif
	}

	pthread_join(com_read_id, & ptr);

	return 0;
}

